<template>
	<view class="news-container">
		<!-- 新增功能1: 选择图片并预览 -->
		<view class="function-section">
			<view class="section-title">作业功能一：选择本机图片并预览</view>

			<view class="function-card">
				<text class="function-title">3.1 选择图片 uni.chooseImage(OBJECT)</text>
				<view class="function-description">
					<text>使用uni.chooseImage()选择图片，uni.previewImage()预览图片</text>
				</view>

				<button @click="chooseImage" class="func-btn primary-btn">选择图片</button>

				<view v-if="selectedImages.length > 0" class="image-preview-area">
					<text class="preview-title">已选择 {{selectedImages.length}} 张图片</text>
					<text class="preview-tip">点击任意图片可预览大图</text>

					<scroll-view class="image-list" scroll-x="true">
						<view v-for="(img, index) in selectedImages" :key="index" class="image-item">
							<image :src="img" mode="aspectFill" @click="previewImage(index)" class="preview-image">
							</image>
							<text class="image-index">图{{index + 1}}</text>
						</view>
					</scroll-view>

					<view class="preview-actions">
						<button @click="previewAllImages" class="func-btn secondary-btn">预览全部</button>
						<button @click="clearImages" class="func-btn warn-btn">清空图片</button>
					</view>
				</view>

				<view v-else class="empty-state">
					<text class="empty-text">暂未选择图片</text>
					<text class="empty-tip">点击上方按钮选择本地图片</text>
				</view>
			</view>
		</view>

		<!-- 原有新闻列表保持不变 -->
		<view class="news-list-section">
			<text class="section-title">新闻列表</text>
			<view v-for="(item, index) in news" :key="item.nID" class="news-item">
				<CardViewText :title="item.title" :isTop="item.isTop" :author="item.author" :comments="item.comments"
					:timedata="item.timedata" :mode="item.mode" :images="item.images" :showSearch="item.showSearch"
					@click="onCardClick(item.nID)">

					<template v-slot:tips v-if="item.showSearch">
						<view class="slotcontent">
							<text>搜索</text>
							<view class="borderbox"><text>今日金价</text></view>
							<view class="borderbox"><text>精选好物</text></view>
						</view>
					</template>
				</CardViewText>
			</view>
		</view>
	</view>
</template>

<script>
	import CardViewText from "../../../components/CardViewText.vue"
	import xinwen from "../../../Data/news.json"
	export default {
		components: {
			CardViewText
		},
		data() {
			return {

				news: null,
				// 新增功能1数据
				selectedImages: [] // 选择的图片列表
			}
		},
		onShow() {
			this.news = xinwen.datalist;
		},
		onLoad() {
			console.log("page onLoad " + this.news);
		},
		methods: {
			onCardClick(nid) {
				console.log('点击了新闻:', nid);
			},

			// 功能1: 选择本机图片 - 根据讲义3.1实现
			chooseImage() {
				uni.chooseImage({
					count: 6, // 默认9，根据讲义设置为6
					sizeType: ['original', 'compressed'], // 原图和压缩图
					sourceType: ['album'], // 从相册选择
					success: (result) => {
						// 通过反馈结果中的tempFilePaths获取图片的本地文件路径列表
						console.log("选择的图片路径:" + JSON.stringify(result.tempFilePaths));
						this.selectedImages = result.tempFilePaths;

						uni.showToast({
							title: `成功选择${result.tempFilePaths.length}张图片`,
							icon: 'success',
							duration: 2000
						});
					},
					fail: (error) => {
						console.error('选择图片失败:', error);
						uni.showToast({
							title: '选择图片失败，请检查权限设置',
							icon: 'none',
							duration: 3000
						});
					}
				});
			},

			// 预览单张图片 - 根据讲义3.2实现
			previewImage(index) {
				console.log('预览图片索引:', index);
				uni.previewImage({
					urls: this.selectedImages,
					current: index,
					indicator: 'default',
					loop: true
				});
			},

			// 预览全部图片
			previewAllImages() {
				if (this.selectedImages.length === 0) {
					uni.showToast({
						title: '请先选择图片',
						icon: 'none'
					});
					return;
				}

				// 直接预览所有图片，从第一张开始
				uni.previewImage({
					urls: this.selectedImages,
					current: 0
				});
			},

			// 清空已选图片
			clearImages() {
				this.selectedImages = [];
				uni.showToast({
					title: '已清空图片',
					icon: 'success'
				});
			}
		}
	}
</script>

<style scoped>
	.news-container {
		padding: 20rpx;
		background-color: #f5f5f5;
		min-height: 100vh;
	}

	.section-title {
		font-size: 36rpx;
		font-weight: bold;
		color: #333;
		margin: 30rpx 0 20rpx 0;
		display: block;
		padding-left: 20rpx;
		border-left: 8rpx solid #007AFF;
	}

	/* 功能区域样式 */
	.function-section {
		margin-bottom: 40rpx;
	}

	.function-card {
		background-color: #fff;
		padding: 30rpx;
		border-radius: 16rpx;
		margin-bottom: 24rpx;
		box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.08);
		border: 2rpx solid #e8f4ff;
	}

	.function-title {
		font-size: 32rpx;
		font-weight: 600;
		color: #007AFF;
		display: block;
		margin-bottom: 15rpx;
	}

	.function-description {
		background-color: #f0f8ff;
		padding: 20rpx;
		border-radius: 8rpx;
		margin-bottom: 25rpx;
		border-left: 4rpx solid #007AFF;
	}

	.function-description text {
		font-size: 24rpx;
		color: #666;
		line-height: 1.6;
	}

	/* 按钮样式 */
	.func-btn {
		border-radius: 12rpx;
		padding: 20rpx 30rpx;
		font-size: 26rpx;
		font-weight: 500;
		margin: 10rpx;
		border: none;
	}

	.primary-btn {
		background-color: #007AFF;
		color: white;
	}

	.secondary-btn {
		background-color: #66b3ff;
		color: white;
	}

	.warn-btn {
		background-color: #ff4d4f;
		color: white;
	}

	/* 图片预览区域 */
	.image-preview-area {
		margin-top: 30rpx;
		padding: 20rpx;
		background-color: #f8f9fa;
		border-radius: 12rpx;
		border: 1rpx dashed #d9d9d9;
	}

	.preview-title {
		font-size: 28rpx;
		font-weight: 600;
		color: #333;
		display: block;
		margin-bottom: 8rpx;
	}

	.preview-tip {
		font-size: 22rpx;
		color: #999;
		display: block;
		margin-bottom: 20rpx;
	}

	.image-list {
		white-space: nowrap;
		margin: 20rpx 0;
	}

	.image-item {
		display: inline-flex;
		flex-direction: column;
		align-items: center;
		margin-right: 25rpx;
	}

	.preview-image {
		width: 140rpx;
		height: 140rpx;
		border-radius: 12rpx;
		border: 3rpx solid #e8f4ff;
		background-color: #fafafa;
	}

	.image-index {
		font-size: 20rpx;
		color: #999;
		margin-top: 8rpx;
	}

	.preview-actions {
		display: flex;
		justify-content: center;
		gap: 20rpx;
		margin-top: 25rpx;
	}

	/* 空状态样式 */
	.empty-state {
		text-align: center;
		padding: 60rpx 20rpx;
		background-color: #fafafa;
		border-radius: 12rpx;
		margin-top: 20rpx;
	}

	.empty-text {
		font-size: 28rpx;
		color: #999;
		display: block;
		margin-bottom: 15rpx;
	}

	.empty-tip {
		font-size: 22rpx;
		color: #ccc;
		display: block;
	}

	/* 原有新闻列表样式 */
	.news-list-section {
		margin-top: 40rpx;
		border-top: 2rpx solid #e8e8e8;
		padding-top: 30rpx;
	}

	.news-item {
		margin-bottom: 24rpx;
	}

	.slotcontent {
		display: flex;
		align-items: center;
		margin-top: 20rpx;
		gap: 16rpx;
		color: #1890ff;
		font-size: 26rpx;
	}

	.borderbox {
		padding: 8rpx 16rpx;
		border: 1rpx solid #e0e0e0;
		border-radius: 8rpx;
		background: #fafafa;
	}
</style>